<?php
namespace app\admin\controller;


use think\Controller;
use app\admin\model\Role as RoleModel;
use think\Db;
use think\Request;
use think\Session;

class Role extends Controller
{
    // 初始化 防止用户未登录就访问页面
    public function _initialize()
    {
        if (!session('nodeList')) {
            return $this->error('您还未登录,请登录',url('admin/index/index'));
        }
    }
    // 首页
    public function index()
    {
        $auth = request()->controller().request()->action();
        $this->judge($auth);

        $role = new RoleModel();
        $list = $role->paginate(10);
        foreach ($list as $value) {
            $value->roles;
        }
        // halt($list);
        return $this->fetch('role/index', [
            'list' => $list
        ]);
    }

    // 更改页面
    public function edit($id)
    {
        $auth = request()->controller().request()->action();
        $this->judge($auth);

        $role = RoleModel::get($id);
        $auth = Db::name('auth')->where('status',1)->select();
        // halt($role);
        return $this->fetch('role/edit', [
            'list' => $role,
            'auth' => $auth
        ]);
    }

    // 更改状态
    public function delete($id)
    {
        $auth = request()->controller().request()->action();
        $this->judge($auth);

        $role = RoleModel::get($id);
        $status = $role->status;
        if ($status == 1) {
            $role->status = 0;
        } else {
            $role->status = 1;
        }
        $role->save();
        return $role->status;
    }

    // 添加处理
    public function save(Request $request)
    {
        // 接收数据
        $list = $request->post();
        // 模型处理
        $role = new RoleModel($list);
        // 验证器验证数据
        $check = $this->validate($list, 'admin/role');
        if (true !== $check) {
            $this->error($check);
            return;
        }
        $role->allowField(true)->save();
        return $role;
    }

    // 更新处理数据
    public function update(Request $request)
    {
        // 接收数据
        $list = $request->put();
        // 模型处理
        $role = RoleModel::get($list['id']);
        $role->roles()->detach();
        if (isset($list['auth'])) {
            $result = $role->roles()->saveAll($list['auth']);
        }
        if ($list['role_name'] != $role['role_name']) {
            $name = RoleModel::get(['role_name' => $role['role_name']]);
            if (!empty($name)) {
                return $this->error('角色名已存在!请更换角色名后重试!');
            }
            $role->role_name = $list['role_name'];
            $result = $role->save();
        }
        if ($result == true) {
            $this->success('更改成功!',  'admin/role/index');
        } else {
            $this->error('更改失败!请重试!');
        }
    }

    /*
     * 判断权限
     */
    public function judge($auth)
    {
        $nodeList = Session::get('nodeList');
        if (empty($nodeList)) {
            return $this->error('您的权限不足!');
        }
        if (!in_array($auth,$nodeList)) {
            return $this->error('您的权限不足!');
        }
    }
}